﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ProductManagement;

public partial class Modules_Purchase_ucAccessoriesList : System.Web.UI.UserControl
{
    Products _accessory = new Products();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["Product_SearchAccessory"] = string.Empty;
            Search();
        }

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["Product_selectedProducts"] = 0;
        Session["Product_SearchAccessory"] = txtSearch.Text;
        Search();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Session["Product_selectedProducts"] = 0;
        Session["Product_Add/EditAccessory"] = "Add";
        Session["UserManager_Control"] = "Production/ucAddAccessory.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");

    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        GetIDs();
        Session["Product_selectedProducts"] = 0;
        Session["Product_Add/EditAccessory"] = "Edit";
        Session["UserManager_Control"] = "Production/ucAddAccessory.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Delete();
        Search();
    }
    protected void btnSuppliers_Click(object sender, EventArgs e)
    {
        GetIDs();
        Session["Product_module"] = "Accessories";
        Session["UserManager_Control"] = "Production/ucVendorList.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }


    protected void lnbBack_Click(object sender, EventArgs e)
    {
        Back();
    }

    void Search()
    {
        try
        {
            string count = Session["Product_selectedProducts"].ToString();
            string productID = string.Empty;
            if (count == "1")
                productID = ((string[])Session["Product_IDs"])[0].ToString();
            DataSet dsAccessory;
            _accessory.SearchAccessory(productID,Session["Product_SearchAccessory"].ToString(), out dsAccessory);
            gvwAccessory.DataSource = dsAccessory;
            gvwAccessory.Columns[1].Visible = true;
            gvwAccessory.DataBind();
            gvwAccessory.Columns[1].Visible = false;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Search Accessories Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    void GetIDs()
    {
        try
        {
            int count = 0;
            string[] ID = new string[gvwAccessory.Rows.Count];

            for (int i = 0; i < gvwAccessory.Rows.Count; i++)
            {
                CheckBox chkGrid = (CheckBox)(gvwAccessory.Rows[i].Cells[0].FindControl("chkEditDelete"));
                if (chkGrid.Checked == true)
                {
                    ID[count] = gvwAccessory.Rows[i].Cells[1].Text;
                    count++;
                }
            }

            Session["Product_IDs"] = ID;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Get ID Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Delete()
    {

        try
        {
            int index = 0;
            string[] AccessoryID = new string[gvwAccessory.Rows.Count];

            for (int i = 0; i < gvwAccessory.Rows.Count; i++)
            {
                CheckBox chkGrid = (CheckBox)(gvwAccessory.Rows[i].Cells[0].FindControl("chkEditDelete"));
                if (chkGrid.Checked == true)
                {
                    AccessoryID[index] = gvwAccessory.Rows[i].Cells[1].Text;
                    index++;
                }
            }


            string conditionDeleteAccessory = "AccessoryID='" + AccessoryID[0] + "'";
            string conditionDeleteProductAccessory = "AccessoryID='" + AccessoryID[0] + "'";

            for (int i = 1; i < index; i++)
            {
                conditionDeleteAccessory = conditionDeleteAccessory + "or AccessoryID='" + AccessoryID[i] + "'";
                conditionDeleteProductAccessory = conditionDeleteProductAccessory + "or AccessoryID='" + AccessoryID[i] + "'";

            }

            _accessory.DeleteAccessory(conditionDeleteAccessory, conditionDeleteProductAccessory);
            Response.Write("<script>alert('Deleted Successfully !')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Delete Accessory Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    void Back()
    {
        Session["UserManager_Control"] = "Production/ucProductList.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void gvwAccessory_RowDataBound(object sender, GridViewRowEventArgs e)
    {    

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";

        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }
}
